/*
///cyberwin_offline_database_printtemp.js
 未来之窗打印模板解析技术 2024-09

  LeftMargin="0" TopMargin="0" RightMargin="0" BottomMargin="0"
  ReportPage
  <ReportPage Name="Page1" PaperWidth="78" PaperHeight="209.8" LeftMargin="0" TopMargin="0" RightMargin="0" BottomMargin="0">
*/
class CyberWin_Database_printtemp_KV {
  constructor() {
    this.data = {};
	this.line_count = 0 ;
	this.pageset = {left:0,right:0,top:0,bottom:0,width:0,height:0};
  }

  解析(数据,打印数据,数据类型) {
   // this.data[key] = value;
   this.line_count = 0 ;
   this.data = {};

  var 数据_未来之窗 = this.数据渲染(数据,打印数据,数据类型);
   const parser = new DOMParser();
     
       const xmlDoc = parser.parseFromString(数据_未来之窗, 'application/xml');
	    console.log("未来之窗打印");
	  // console.log(xmlDoc);

	   const targetElement = xmlDoc.getElementsByTagName('Report')[0];
		if (targetElement) {
		 // console.log(targetElement.textContent);
		}

	   const rootElement = xmlDoc.getElementsByTagName('Report')[0];
	   const childElements = rootElement.getElementsByTagName('ReportPage')[0];
	   console.log("打印2=ReportPage");

	   //var Font = childElements_打印[j].getAttribute('Font');
	   //var Font = childElements_打印[j].getAttribute('Font');
	   //PaperWidth="78" PaperHeight="209.8" LeftMargin="0" TopMargin="0" RightMargin="0" BottomMargin="0"
	    var 页面宽度=childElements.getAttribute('PaperWidth');
		var 页面高度=childElements.getAttribute('PaperHeight');
		var 页面left=childElements.getAttribute('LeftMargin');
		var 页面top=childElements.getAttribute('TopMargin');
		var 页面right=childElements.getAttribute('RightMargin');
		var 页面bottom=childElements.getAttribute('BottomMargin');
		this.pageset = {left:页面left,right:页面right,top:页面top,bottom:页面bottom,width:页面宽度,height:页面高度};

	    console.log(childElements);
			const childElements_页面 = childElements.childNodes;
			for (let i = 0; i < childElements_页面.length; i++) {
              console.log("打印3=ReportPage="+childElements_页面[i].nodeType+",Node.ELEMENT_NODE="+Node.ELEMENT_NODE);
			   console.log(childElements_页面[i]);
			  // console.log(childElements_页面[i].tagName + ': ' + childElements_页面[i].textContent);
			 // if (childElements_页面[i].nodeType === Node.ELEMENT_NODE) {
			 // if (childElements_页面[i].nodeType ===3) {
              if (childElements_页面[i] != "#text") {
				console.log("打印4="+childElements_页面[i].tagName );

				 if("DataBand" == childElements_页面[i].tagName || "PageHeaderBand"  == childElements_页面[i].tagName){
					 const childElements_打印 = childElements_页面[i].childNodes;
					 for (let j = 0; j < childElements_打印.length; j++) {

						 console.log("打印明细");
						  console.log("打印4 nodeType="+childElements_打印[j].nodeType);
						  if (childElements_打印[j] == "#text") {
							  console.log("打印5，明细不满足");
							  continue;
						  }
						  if (childElements_打印[j].nodeType ==3) {
							  console.log("删除空行");
							  continue;
						  }

						 console.log(childElements_打印[j]);
						 var Font = childElements_打印[j].getAttribute('Font');
						 var CanGrow = childElements_打印[j].getAttribute('CanGrow');
						 
						  var content = childElements_打印[j].getAttribute('Text');

						  //Left
						  var x =childElements_打印[j].getAttribute('Left');
						  var y =childElements_打印[j].getAttribute('Top');
                            console.log(content);
                        //Font="宋体, 9pt"
						  var 字体分离array = 	 Font.split(', ');
						  var 字体_大小 =字体分离array[1].replace('pt','');

						  if(x){
						  }else{
							  x=0;
						  }
						  if(y){
						  }else{
							  y=0;
						  }
						  if(CanGrow){
							  CanGrow ="Y";
						  }else{
							  CanGrow = "N";
						  }
						 
						 console.log(childElements_打印[j].tagName + '，Top: ' + Font +" left="+x);

						 var 打印条目 ={type:'text',x:x,y:y,content:content,CanGrow:CanGrow,Font:Font,Fontsize:字体_大小};


						  this.data[this.line_count] = 打印条目;
						  this.line_count=this.line_count+1;
					 }
				 }
			  }
			}

	 return {count:this.line_count,data:this.data,pageset:this.pageset};
  }
   
    数据渲染( 模板, 打印数据_mix,数据类型 ){
		var 打印数据 ={};
		if( 数据类型 == "wlzc"){
			const parser = new DOMParser();
			const 打印数据_xmlDoc = parser.parseFromString(打印数据_mix, "text/xml");
			打印数据 = this.XML抓换JSON(打印数据_xmlDoc.documentElement);
		}else{
			打印数据 = 打印数据_mix;
		}
		var 数据keys = Object.keys(打印数据);
		var 打印模板数据 =模板;
		for(var i = 0; i<数据keys.length; i++){
			var 字段key = 数据keys[i];
			var 字段value = 打印数据[数据keys[i]];
			var 字段key_name= "[Table1."+字段key+"]";
			打印模板数据 = 打印模板数据.replace(字段key_name,字段value);
		}
		  console.log("打印数据渲染");
	      console.log(打印模板数据);

		return 打印模板数据;


	}

	 XML抓换JSON(  打印数据xml ){
		 var xml = 打印数据xml;
		  let 打印数据 = {};
		  if (xml.nodeType === 1) {
			if (xml.attributes.length > 0) {
			  打印数据["@attributes"] = {};
			  for (let j = 0; j < xml.attributes.length; j++) {
				const attribute = xml.attributes.item(j);
				//打印数据["@attributes"][attribute.nodeName] = attribute.nodeValue;
				打印数据["@attributes"] = attribute.nodeValue;
			  }
			}
		  } else if (xml.nodeType === 3) {
			打印数据 =  xml.nodeValue;
			 console.log("json--json");
			  console.log(打印数据);
			//return  xml.nodeValue;
		  }

		  if (xml.hasChildNodes()) {
			  console.log(xml);
			for (let i = 0; i < xml.childNodes.length; i++) {
			  const item = xml.childNodes.item(i);
			  console.log("item");
			  console.log(item);
			  const nodeName = item.nodeName;
			  console.log("nodeName="+nodeName);
			   console.log(打印数据);
			  if (打印数据[nodeName]) {
				if (!Array.isArray(打印数据[nodeName])) {
				  //const old = 打印数据[nodeName];
				  //打印数据[nodeName] = [old];
				}
			   // 打印数据[nodeName].push(XML抓换JSON(item));
				//打印数据[nodeName]=XML抓换JSON(item);
				
				if("#text" ==nodeName){
					打印数据=item.nodeValue;
				}else{
					打印数据=this.XML抓换JSON(item);
				}
			  } else {
				//打印数据[nodeName] = XML抓换JSON(item);
				//打印数据=XML抓换JSON(item);
				if("#text" ==nodeName){
					打印数据=item.nodeValue;
				}else{
					打印数据[nodeName] = this.XML抓换JSON(item);
				}
			  }
			}
		  }

			 let objectLength = 0;
				for (let key in 打印数据) {
				  if (打印数据.hasOwnProperty(key)) {
					objectLength++;
				  }
				}

		  if(objectLength<1){ 
			  return "";
		}
		  
		  return 打印数据;

	 


	}


  

}